import com.arcadedb.database.Database;
import com.arcadedb.database.DatabaseFactory;
import com.arcadedb.database.RID;
import com.arcadedb.graph.Vertex;
import com.arcadedb.query.sql.executor.Result;
import com.arcadedb.query.sql.executor.ResultSet;

import java.util.Optional;

public class createLink {

    public static void main(String[] args){
        DatabaseFactory dbFactory=new DatabaseFactory("./databases/test");
        Database db=dbFactory.open();

        long tot=0;

        db.begin();

        //db.command("SQL", "CREATE INDEX ON SnapShotTest(id) NOTUNIQUE");
        //db.command("SQL", "CREATE INDEX ON SnapShotPureOrder(id) NOTUNIQUE");
//        db.command("SQL", "TRUNCATE TYPE succ");
//        ResultSet succList=db.query("SQL", "SELECT FROM succ");
//        if (succList.hasNext()){
//            System.out.println("failed to truncate type succ");
//            db.rollback();
//            db.close();
//            return;
//        }
//
//        db.command("SQL", "DROP TYPE succ");
//        db.getSchema().createEdgeType("succ");
//
//        db.commit();
//        db.begin();

//        db.command("SQL", "DELETE FROM SnapShotTest WHERE id=succId");
//        ResultSet rsErrSnapShotTestList=db.query("SQL", "SELECT FROM SnapShotTest WHERE id=succId");
//        if (rsErrSnapShotTestList.hasNext()){
//            System.out.println("failed to clean SnapShotTest");
//            return;
//        }

        long offset=0, size=10000;
        int count=1;
        while (count>0) {
            long startTime=System.currentTimeMillis();
            ResultSet rsSnapShotTestList = db.query("SQL", String.format("SELECT FROM SnapShotTest SKIP %d LIMIT %d", offset, size));
            count=0;
            while (rsSnapShotTestList.hasNext()) {
                count++;
                Result result = rsSnapShotTestList.next();

                Optional<Vertex> optPred=result.getVertex();
                if (optPred.isEmpty()) continue;;
                Vertex pred=optPred.get();
                //System.out.println(pred);

                String id = result.getProperty("id");
                String succId = result.getProperty("succId");
                if (succId == null || succId.equals(id)) continue;
                //System.out.println(succId);

                ResultSet rsSucc = db.query("SQL", String.format("SELECT FROM SnapShotTest WHERE id='%s'", succId));
                if (!rsSucc.hasNext()) {
                    //System.err.printf("[ERROR] cannot find valid vertex with id %s, skipped\n", succId);
                    continue;
                }

                while (rsSucc.hasNext()) {
                    Optional<Vertex> optSucc = rsSucc.next().getVertex();
                    if (optSucc.isEmpty()) continue;
                    Vertex succ = optSucc.get();

                    RID succRid = succ.getIdentity(), predRid = pred.getIdentity();
                    if (succRid.equals(predRid)) {
                        System.err.printf("[WARNING] skipping circle %s -> self\n", succId);
                        continue;
                    }

                    //System.out.println(String.format("Creating edge from %s[succId=%s] to %s[id=%s]", predRid, pred.getString("succId"), succRid, succ.getString("id")));
                    pred.newEdge("succ", succ, true);
                }
            }
            db.commit();
            long elapsed=System.currentTimeMillis()-startTime;
            db.begin();
            System.out.printf("Creating edges : %d~%d (%dms)\n", offset, offset+count-1, elapsed);
            tot+=elapsed;
            offset+=size;
        }

        db.commit();
//        succList=db.query("SQL", "SELECT FROM succ");
//        while (succList.hasNext()){
//            Optional<Edge> optEdge= succList.next().getEdge();
//            if (optEdge.isEmpty()) continue;
//
//            Edge edge=optEdge.get();
//            System.out.println(edge.getOutVertex()+" -> "+edge.getInVertex());
//        }
        db.close();
        System.out.println(tot+"ms");
    }
}
